/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.inv.controller;

import java.util.Map;
import java.util.List;

import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.dian.common.annotation.SysLog;
import com.dian.modules.inv.poi.vo.GoodsStockInitExportVO;
import com.dian.modules.inv.poi.verify.GoodsStockInitExcelVerifyHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.dian.common.controller.AbstractController;
import com.dian.modules.inv.entity.GoodsStockEntity;
import com.dian.modules.inv.service.GoodsStockService;
import com.dian.modules.inv.poi.vo.GoodsStockExportVO;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 库存表
 *
 * @author xiaolai
 * @email xiaolai@9dyun.cn
 * @date 2019-06-29 15:53:12
 */
@RestController
@Api("库存表接口")
@RequestMapping("inv/goodsStock")
public class GoodsStockController extends AbstractController{
    @Autowired
    private GoodsStockService goodsStockService;


    @Autowired
    private GoodsStockInitExcelVerifyHandler goodsStockInitExcelVerifyHandler;
    /**
     * 库存表列表
     */
    @SysLog("库存表列表")
    @GetMapping("/list")
    @ApiOperation("库存表列表")
    @RequiresPermissions("inv:goodsStock:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = goodsStockService.queryPage(params);

        return R.ok().put("page", page);
    }






    /**
     * 库存表导出
     */
    @SysLog("库存表导出")
    @ApiOperation("库存表导出")
    @GetMapping("/export")
    @RequiresPermissions("inv:goodsStock:export")
    public void export(@RequestParam Map<String, Object> params){

        List<GoodsStockExportVO> list = goodsStockService.exportList(params);

        exportExcel(list,"库存表",GoodsStockExportVO.class);
    }

    /**
     * 库存表导出
     */
    @SysLog("库存表导出")
    @ApiOperation("库存表导出")
    @GetMapping("/init-export")
    @RequiresPermissions("inv:goodsStock:export")
    public void initExport(@RequestParam Map<String, Object> params){

        List<GoodsStockInitExportVO> list = goodsStockService.initExport(params);

        exportExcel(list,"初始化库存表",GoodsStockInitExportVO.class);
    }

    /**
     * 库存表导入
     */
    @SysLog("库存表导入")
    @ApiOperation("库存表导入")
    @PostMapping("/init-import")
    @RequiresPermissions("inv:goodsStock:export")
    public R upload(@RequestParam("file") MultipartFile file) {
        ExcelImportResult<GoodsStockInitExportVO> excelImportResult=this.importExcel(file,GoodsStockInitExportVO.class, goodsStockInitExcelVerifyHandler);
        if(CollectionUtils.isNotEmpty(excelImportResult.getList())){
            goodsStockService.initImport(excelImportResult.getList());
        }
        if(excelImportResult.isVerfiyFail()){
            return R.error(excelImportResult.getMap());
        }else{
            return R.ok();
        }

    }

}
